package com.edgclub.edg.uc;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.edgclub.edg.R;

/**
 * 赏金币的对话框
 * 
 *
 */
public class GoldDialog extends Dialog {

	private Button tv1,tv2,tv3;
	private TextView tvOk, tv_num ,other_gold;
	private ImageView imgv_close;

	private Context context;

	private Dialog dialog;

	private GoldDialog instance;
	private LinearLayout change_gold;
	private EditText gold_num;
	private int gold = 5; //金币值

	public static GoldDialog create(Context context, boolean hasCancle) {
		GoldDialog instance = new GoldDialog(context, hasCancle);
		return instance;

	}

	private GoldDialog(Context context, boolean hasCancle) {
		super(context, R.style.MyAlertDialog);
		init(context, hasCancle);
	}

	private void init(Context context, boolean hasCancle) {
		this.context = context;
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.width = context.getResources().getDisplayMetrics().widthPixels;
		View contentView = LayoutInflater.from(context).inflate(
				R.layout.layout_gold_dialog, null);

		setContentView(contentView, lp);
		tvOk = (TextView) contentView.findViewById(R.id.tv_ok);
		other_gold = (TextView) contentView.findViewById(R.id.other_gold);
		tvOk = (TextView) contentView.findViewById(R.id.tv_ok);
		tv1 = (Button) contentView.findViewById(R.id.tv1);
		tv2 = (Button) contentView.findViewById(R.id.tv2);
		tv3 = (Button) contentView.findViewById(R.id.tv3);
		tv_num = (TextView) contentView.findViewById(R.id.tv_num);
		change_gold = (LinearLayout) contentView.findViewById(R.id.change_gold);
		gold_num = (EditText) contentView.findViewById(R.id.gold_num);
		imgv_close = (ImageView) contentView.findViewById(R.id.imgv_close);
		tv1.setSelected(true);
		tv1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				tv1.setSelected(true);
				tv2.setSelected(false);
				tv3.setSelected(false);
				gold = 5;
			}
		});
		tv2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				tv1.setSelected(false);
				tv2.setSelected(true);
				tv3.setSelected(false);
				gold = 10;
			}
		});
		tv3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				tv1.setSelected(false);
				tv2.setSelected(false);
				tv3.setSelected(true);
				gold = 15;
			}
		});
		other_gold.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				gold = 0;
				change_gold.setVisibility(View.GONE);
				gold_num.setVisibility(View.VISIBLE);
				other_gold.setVisibility(View.GONE);
			}
		});
		gold_num.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
				String text = charSequence.toString();
				if (text.contains(".")) {
					int index = text.indexOf(".");
					if (index + 2 < text.length()) {
						text = text.substring(0, index + 2);
						gold_num.setText(text);
						gold_num.setSelection(text.length());
					}

				}

			}

			@Override
			public void afterTextChanged(Editable editable) {

			}
		});
		imgv_close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				dismiss();
			}
		});

		this.setCanceledOnTouchOutside(true);
	}

	public TextView getTv_num() {
		return tv_num;
	}

	public void setTv_num(TextView tv_num) {
		this.tv_num = tv_num;
	}

	/**
	 * 设置确认的监听
	 */
	public void setSubmitListener(
			final SubmitClickListener onSubmitClickListener) {
		tvOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				if (null != onSubmitClickListener) {
					if (!TextUtils.isEmpty(gold_num.getText().toString())){
						gold=Integer.parseInt(gold_num.getText().toString());
						String amountText = gold_num.getText().toString();

						if(Double.valueOf(amountText)>100||Double.valueOf(amountText)<1){
							Toast.makeText(context,"打赏的金币在1~100之间",Toast.LENGTH_SHORT).show();
							return;
						}

					}else if(gold==0){
						Toast.makeText(context,"请输入打赏的金币数量",Toast.LENGTH_SHORT).show();
						return;
					}
					onSubmitClickListener.submitClick(gold);
				}
			}
		});
	}

	public interface SubmitClickListener {
		public void submitClick(int gold);

	}


	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.dismiss();
	}
}
